<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class HomeController extends Controller {

	/* 空操作，用于输出404页面 */
	public function _empty(){
		$this->redirect('Index/index');
	}


    protected function _initialize(){
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
    }

	/* 用户登录检测 */
	protected function login(){
		/* 用户登录检测 */
		is_login() || $this->error('您还没有登录，请先登录！', U('User/login'));
	}

    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    function juhecurl($url, $params = false, $ispost = 0)    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }
    /*
     * 0点整自动释放积分 时间范围默认60秒内
     * */
    public function checktime($len = 60){
        $result = false;
        if( time() - strtotime(date("Y-m-d")) < $len){
            $result = true;
        }
        return $result;
    }
    /*
     * 余额日志
     * */
    public function balance_log(){
        $tokk = $_SERVER['HTTP_TOKEN'];
        $toke = json_decode(base64_decode($tokk),true);//解密token
        $mobile = $toke['mobile'];//赋值
        $where = array('mobile' => $mobile);
        $balance_of = $_POST['balance'];//需要转为积分的余额
        $balance = M('personal')->where($where)->field('balance,username,id')->find();//转换前的余额
        $balance_v = $balance['balance'] - $balance_of;//转换后的余额
		$array = array(
            'balance' => $balance['balance'],
            'balance_v' => $balance_v,
            'balance_of' => $balance_of,
            'update_time' => time(),
            'balance_from' => $balance['username'],
            'user_id' => $balance['id'],
            'username' => $balance['username']);
		if(empty($balance['balance']) &&
            empty($balance_v) &&
            empty($balance_of) &&
            empty($balance['username']) &&
            empty($balance['id'])){
            return false;
        }else{
			if($balance_of > 0){
				$data = M('balance_log')->add($array);
				if($data){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}    
		}
    }
    /*
     *积分日志
     * */
    public function integral_log(){
        $tokk = $_SERVER['HTTP_TOKEN'];
        $toke = json_decode(base64_decode($tokk),true);//解密token
        $mobile = $toke['mobile'];//赋值
        $where = array('mobile' => $mobile);
        $balance = M('personal')->where($where)->field('username,id')->find();//转出者的余额和昵称
        $integral_of = $_POST['balance'] * C('YUE_INTO_JIFEN');//收到的积分
        $integral = M('personal')->where($where)->field('integral')->find();//转换前的积分
        $integral_v = $integral['integral'] + $integral_of;
        $array = array(
            'integral' => $integral['integral'],
            'integral_v' => $integral_v,
            'integral_of' => $integral_of,
            'update_time' => time(),
            'integral_from' => $balance['username'],
            'user_id' => $balance['id'],
			'username' => $balance['username']);
		if(empty($integral['integral']) &&
            empty($integral_v) &&
            empty($integral_of) &&
            empty($balance['username']) &&
            empty($balance['id'])){
            return false;
        }else{
			if($integral_of){
				$data = M('integral_log')->add($array);
				if($data){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
	}
    /*
     *转余额至他人积分日志
     * */
    public function integral_log_out(){
        $tokk = $_SERVER['HTTP_TOKEN'];
        $toke = json_decode(base64_decode($tokk),true);//解密token
        $mobile = $toke['mobile'];//赋值
        $where = array('mobile' => $mobile);
        $balance = M('personal')->where($where)->field('username,id')->find();//转出者的余额和昵称
        $integral_of = $_POST['balance'] * C('YUE_INTO_JIFEN');//收到的积分
        $integral = M('personal')->where(array('mobile' => $_POST['mobile_out']))->field('integral')->find();//转换前的积分
        $integral_v = $integral['integral'] + $integral_of;
		
		$phone = $_POST['mobile_out'];//转入者的电话号码
		$phone_into = M('personal')->where(array('mobile' => $phone))->field('id,username')->find();
		
        $array = array(
            'integral' => $integral['integral'],
            'integral_v' => $integral_v,
            'integral_of' => $integral_of,
            'update_time' => time(),
            'integral_from' => $balance['username'],
            'user_id' => $phone_into['id'],
			'username' => $phone_into['username']);
		if(empty($integral['integral']) &&
            empty($integral_v) &&
            empty($integral_of) &&
            empty($balance['username']) &&
            empty($phone_into['id']) &&
			empty($phone_into['username'])){
            return false;
        }else{
			$data = M('integral_log')->add($array);
			if($data){
				return true;
			}else{
				return false;
			}
		}
	}
	/*
     * 转余额至他人积分/余额日志
     * */
    public function balance_log_out(){
        $tokk = $_SERVER['HTTP_TOKEN'];
        $toke = json_decode(base64_decode($tokk),true);//解密token
        $mobile = $toke['mobile'];//赋值
        $where = array('mobile' => $mobile);
        $balance_of = $_POST['balance'];//需要转为积分的余额
        $balance = M('personal')->where($where)->field('balance,username,id')->find();//转换前的余额
        $balance_v = $balance['balance'] - $balance_of;//转换后的余额
		
		$array = array(
            'balance' => $balance['balance'],
            'balance_v' => $balance_v,
            'balance_of' => $balance_of,
            'update_time' => time(),
            'balance_from' => $balance['username'],
            'user_id' => $balance['id'],
            'username' => $balance['username']);
			if(empty($balance['balance']) &&
				empty($balance_v) &&
				empty($balance_of) &&
				empty($balance['username']) &&
				empty($balance['id'])){
				return false;
			}else{
				$data = M('balance_log')->add($array);
				if($data){
					return true;
				}else{
					return false;
				}
			}
    }
}
